//
//  View+Border.swift
//  BasicTools
//
//  Created by S JZ on 2025/5/28.
//

import SwiftUI

struct ViewBorder: ViewModifier {
    var cornerRadius: CGFloat = 0
    var borderColor: Color = .clear
    var borderWidth: CGFloat = 0
    var backgroundColor: Color = .clear
    var insets: EdgeInsets = EdgeInsets()
    
    init(cornerRadius: CGFloat, 
         borderColor: Color, 
         borderWidth: CGFloat, 
         backgroundColor: Color, 
         insets: EdgeInsets) {
        self.cornerRadius = cornerRadius
        self.borderColor = borderColor
        self.borderWidth = borderWidth
        self.backgroundColor = backgroundColor
        self.insets = insets
    }
    
    func body(content: Content) -> some View {
        content
            .padding(insets)
            .background(
                RoundedRectangle(cornerRadius: cornerRadius, style: .circular)
                    .stroke(borderColor, lineWidth: borderWidth)
                    .background(backgroundColor)
                    .cornerRadius(cornerRadius)
                    .clipped()
            )
            .contentShape(Rectangle())
    }
}

extension BasicWrapper where Base: View {
    public func viewBorder(cornerRadius: CGFloat = 0,
                           borderColor: Color = .clear,
                           borderWidth: CGFloat = 0,
                           backgroundColor: Color = .clear,
                           insets: EdgeInsets = EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)) -> some View {
        
        base.modifier(ViewBorder(cornerRadius: cornerRadius,
                                 borderColor: borderColor,
                                 borderWidth: borderWidth,
                                 backgroundColor: backgroundColor,
                                 insets: insets))
    }
}


